package com.cfh.springcloudclientapplication.controller;

import com.cfh.springcloudclientapplication.stream.SimpleMessageSource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class MessageController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired(required = false)
    private SimpleMessageSource simpleMessageSource;


    @GetMapping("/send")
    public String send(@RequestParam String message) {
        rabbitTemplate.convertAndSend(message);
        return "OK";
    }

    @GetMapping("/stream/send")
    public boolean streamSend(@RequestParam String message) {
        MessageChannel messageChannel = simpleMessageSource.output();
        Map<String, Object> headers = new HashMap<>();
        headers.put("charset-encoding","UTF-8");
        return messageChannel.send(new GenericMessage<>(message,headers));
    }

    @GetMapping("/stream/send/rockemq")
    public boolean streamRocketMQSend(@RequestParam String message) {
        MessageChannel messageChannel = simpleMessageSource.testOutput();
        return messageChannel.send(new GenericMessage<>(message));
    }
}
